package org.huawei260.TiKu.ODsuanfa;

import java.util.*;

/**
 * 本题就是LeetCode - 503 下一个更大元素 II_正整数组nums 下一个更大元素 没有
 * 输出0_伏城之外的博客-CSDN博客
 * 的换皮题，
 *
 */
public class B_2023B_2653__单调栈阿里巴巴找黄金宝箱4 { // 80% python:100%
//	public class Main {
		public static void main(String[] args) {
			Scanner sc = new Scanner(System.in);
			int[] arr = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).
				toArray();
			System.out.println(getResult(arr));
		}
		public static String getResult(int[] arr) {
			LinkedList<int[]> stack = new LinkedList<>();
			int[] res = new int[arr.length];
			Arrays.fill(res, -1);
			findNextBig(arr, stack, res);
			if (stack.size() != 1) findNextBig(arr, stack, res);
			StringJoiner sj = new StringJoiner(",");
			for (int v : res) {
				sj.add(v + "");
			}
			return sj.toString();
		}
		public static void findNextBig(int[] arr, LinkedList<int[]> stack, int[] res) {
			for (int i = 0; i < arr.length; i++) {
				int ele = arr[i];
				while (true) {
					if (stack.size() == 0) {
						stack.add(new int[] {ele, i});
						break;
					} else {
						int[] peek = stack.get(stack.size() - 1);
						int peekEle = peek[0];
						int peekIdx = peek[1];
						if (ele > peekEle) {
							res[peekIdx] = ele;
							stack.removeLast();
						} else {
							stack.add(new int[] {ele, i});
							break;
						}
					}
				}
			}
		}
	}

/**
 * B_2023B_2653__单调栈阿里巴巴找黄金宝箱4
 * # 输入获取
 * arr = list(map(int, input().split(",")))
 * def findNextBig(arr, stack, res):
 *     for i in range(len(arr)):
 *         ele = arr[i]
 *         while True:
 *             if len(stack) == 0:
 *                 stack.append([ele, i])
 *                 break
 *             else:
 *                 peekEle, peekIdx = stack[-1]
 *                 if ele > peekEle:
 *                     res[peekIdx] = ele
 *                     stack.pop()
 *                 else:
 *                     stack.append([ele, i])
 *                     break
 * # 算法入口
 * def getResult():
 *     stack = []
 *     res = [-1] * len(arr)
 *     findNextBig(arr, stack, res)
 *     if len(stack) != 1:
 *         findNextBig(arr, stack, res)
 *     return ",".join(map(str, res))
 * # 算法调用
 * print(getResult())
 */
